---@class RedRob.EffectBase
---@field New fun() : RedRob.EffectBase
local EffectBase = RedRob.CreateClass("EffectBase")
function EffectBase:ctor(id,_type)
    self.id = id or 0
    self.effectType = _type
end
function EffectBase:OnCreate()
    self.resName = ""
    self.loadRequestId = 0
    ---@type Transform
    self.bindTran = nil
    self.isPlaying = false
    self.animName = ""
    ---@type GameObject
    self.obj = nil
    self.isDestroy = false
    self.isActive = true
    self.isLoading = false
    self.pos = Vector3.GetCacheVec()
    ---@type RedRob.Player
    self.casterId = 0   --施法者
    self.targetId = 0   --目标对象
    self.skillId = 0    --产生于哪个技能id
    self.during = -1
    ---@type Spine.Unity.SkeletonAnimation
    self.anim = nil
    self.isLeft = true
    self.curTime = 0
    self.isDie = false
    ---@type UnityEngine.Renderer
    self.render = nil
    self.sortingOrder = 0
    self.followId = 0
    self.color = nil
    self.isAutoDestroy = true
end
function EffectBase:OnDestroy()
    self.isDestroy = true
    self.isDie = true
    RedRob.PoolMgr.CancelLoad(self.loadRequestId)
    RedRob.PoolMgr.ReturnPrefabToPool(self.resName,self.obj)
    --if self.obj then
    --    destroy(self.obj)
    --end
    Vector3.ReturnToPool(self.pos)
    self.pos = nil
    self.casterId = 0
    self.targetId = 0
    self.skillId = 0
    self.render = nil
    self.loadRequestId = 0
    self.anim = nil
    self.obj = nil
    if self.registerOrderId then
        local actor = RedRob.ActorManager.GetActor(self.registerOrderId)
        if actor then
            actor:UnRegisterOrderChanged(self.UpdateOrder,self)
        end
        self.registerOrderId = nil
    end
end
function EffectBase:IsDie()
    return self.isDie
end
function EffectBase:Die()
    self.isDie = true
    --if self.isAutoDestroy then
        --RedRob.EffectMgr.DestroyEffect(self)
    --else
    --    self:SetActive(false)
   -- end
end
function EffectBase:IsPlaying()
    return self.isPlaying
end
function EffectBase:SetAutoDestroy(isAuto)
    self.isAutoDestroy = isAuto
end
function EffectBase:SetSkillId(skillId)
    self.skillId = skillId
end
---@param actor RedRob.Pawn
function EffectBase:RegisterOrderChanged(actor,updateImmediate)
    if not actor then return end
    self.registerOrderId = actor:GetId()
    actor:RegisterOrderChanged(self.UpdateOrder,self)
    if updateImmediate then
        self:UpdateOrder(actor:GetSortingOrder())
    end
end
function EffectBase:UpdateOrder(order)
    self.sortingOrder = order
    if self.render then
        self.render.sortingOrder = order + RedRob.Config.SortingOrder.effect
    end
end

function EffectBase:Reset()
    self.isPlaying = false
    self.isDie = false
    self:SetActive(true)
end
function EffectBase:Play()
    local caster = self:GetCaster()
    if not caster then return end
    local isLeftDir = caster.isLeft
    self.isPlaying = true
    if self.resName == "" then
        return
    end
    self.curTime = 0
    self.isDie = false
    self:SetActive(true)
    self.isLeft = isLeftDir
    self:PlayAnim(isLeftDir)
    self:SetObjPos(self.pos)
    self:UpdateOrder(self.sortingOrder)
    if self.isLoading then return end
    self.isLoading = true
    --resMgr:LoadModelPrefab(self.resName,function(obj1)
    self.loadRequestId = RedRob.PoolMgr.LoadPrefabFromPool(self.resName,self.LoadCallback,self)
end
function EffectBase:LoadCallback(obj)
    self.obj = obj
    self.render = obj:GetComponentInChildren(typeof(UnityEngine.Renderer))
    local anim = obj:GetComponentInChildren(TSkeletonAnimation)
    if anim then
        Util.Lua_SetLocalScaleXYZ(anim.transform,1,1,1)
        self.anim = anim
    end
    self:UpdateOrder(self.sortingOrder)
    local tran = obj.transform
    local scale = RedRob.ClientBattle.PLAYER_SCALE
    Util.Lua_SetLocalScaleXYZ(tran,scale,scale,scale)
    Util.Lua_SetLocalPositionXYZ(tran,self.pos.x,self.pos.y,self.pos.z)
    RedRob.SceneMgr.AddEffectChild(tran)
    self:PlayAnim(self.isLeft)
    if self.followId == 0 then
        self:SetActive(true)
    else
        self:SetActive(false)
    end
end
function EffectBase:PlayAnim(isLeftDir)
    if self.animName ~= "" and self.anim then
        Util.SetFlipX(self.anim, isLeftDir)
        local time = Util.PlayAction(self.anim,self.animName,true,-1,true)
        if self.during == -1 then
            self.during = time
        end
    end
end
---@return RedRob.Skill
function EffectBase:GetSkill()
    local caster = self:GetCaster()
    local skill = caster.skillMgr:GetSkill(self.skillId)
    return skill
end
---@return RedRob.Player
function EffectBase:GetCaster()
    return RedRob.ActorManager.GetActor(self.casterId)
end
---@return RedRob.Player
function EffectBase:GetTarget()
    return RedRob.ActorManager.GetActor(self.targetId)
end

function EffectBase:SetObjPos(pos)
    if self.obj then
        Util.Lua_SetLocalPositionXYZ(self.obj,pos.x,pos.y,pos.z)
    end
end

function EffectBase:SetActive(active)
    if self.isActive == active then
        return
    end
    self.isActive = active
    if self.obj then
        self.obj:SetActive(active)
    end
end
function EffectBase:Follow()
    if self.followId == 0 then return end
    local follow = RedRob.ActorManager.GetActor(self.followId)
    if not follow then return end
    self:SetActive(true)
    self:SetObjPos(follow:GetPos())
end
function EffectBase:SetDuring(during)
    self.during = during
    self.curTime = 0
end
function EffectBase:SetColor(r,g,b,a)
    if not self.color then
        self.color = Color.New(r,g,b,a)
    else
        self.color:Set(r,g,b,a)
    end
end
function EffectBase:SetTrailColor(r,g,b,a)
    self:SetColor(r,g,b,a)
    if not self.obj then return end
    ---@type UnityEngine.TrailRenderer
    local trail = self.obj:GetComponentInChildren(typeof(UnityEngine.TrailRenderer), true)
    if trail then
        trail.startColor = self.color
        trail.endColor = self.color
    end
end
function EffectBase:Update(dt)
    self.curTime = self.curTime + dt
    if self.during ~= -1 then
        if self.curTime >= self.during then
            self:Die()
            return
        end
    end
    self:Follow()
end
